package NextClass.Native;

import vm.runtime.NextVMRuntime;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class NextFile implements Serializable {
    public static String[] fileClassMethods = {
            "exists",
            "is_file",
            "new_file",
            "new_dir",
            "del",
            "abs"
    };
    public static Object run(String Invoke, NextVMRuntime nextVMRuntime, Object object, Object... objects) throws Exception{
        File file = (File) object;
        if (Invoke.equals("exists")) {
            Path path = Paths.get(file.getAbsolutePath().replaceAll("\0", ""));
            //System.out.println(file.exists()+" "+file.getAbsolutePath()+" "+file.isFile());
            return Files.exists(path);
        }
        else if (Invoke.equals("is_file")) {
            return file.isFile();
        }
        else if (Invoke.equals("new_file")) {
            return file.createNewFile();
        }
        else if (Invoke.equals("new_dir")) {
            //return new File(file.getAbsolutePath()).mkdir();
            return file.mkdirs();
        }
        else if (Invoke.equals("del")) {
            return file.delete();
        }
        else if (Invoke.equals("abs")) {
            return file.getAbsolutePath();
        }
        else {
            return null;
        }
    }
}
